<?php
set_error_handler('errorHandler');
function errorHandler($eNo, $msg, $file, $line)
{
	$eType = array(
		E_COMPILE_ERROR => 'E_COMPILE_ERROR',
		E_COMPILE_WARNING => 'E_COMPILE_WARNING',
		E_CORE_ERROR => 'E_CORE_ERROR',
		E_CORE_WARNING => 'E_CORE_WARNING',
		E_ERROR => 'E_ERROR',
		E_NOTICE => 'E_NOTICE',
		E_PARSE => 'E_PARSE',
		E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
		E_STRICT => 'E_STRICT',
		E_USER_ERROR => 'E_USER_ERROR',
		E_USER_NOTICE => 'E_USER_NOTICE',
		E_USER_WARNING => 'E_USER_WARNING',
		E_WARNING => 'E_WARNING'
	);
	
	if($eNo == E_STRICT) return;
	
	$msg = "<b>$eType[$eNo]:</b> $msg in $file on line $line";
	throw new Exception($msg);
}

try{
error_reporting(E_ALL^E_NOTICE);

$curDir = dirname(__FILE__);
$frameworkPath = $curDir . '/../../library/framework/'; 
require_once $frameworkPath . 'CF.php';
require_once $frameworkPath . 'CF/Loader.php';

# disable magic quote gpc
CF::disableMagicQuote();

# add include path to framework
CF_Loader::addIncludePath($frameworkPath);

# register autoload
CF_Loader::registerAutoload();

#application path
$appPath = $curDir . '/../../application/';

# database config
$configReader = new CF_Storage_Ini($appPath . 'config/config.ini', 'dev', false);

# create database connection
$connection = new CF_Db_Connection('default', $configReader->database);
CF_Db::getInstance()->addConnection($connection);
CF_Db::getInstance()->traceQueryError = true;

$sessionSaveHander = new CF_Session_SaveHandler_Db('sessions');
CF_Session::setSaveHander($sessionSaveHander);
CF_Session::start();

# set layout
CF_App::getInstance()->getView()->setLayout('default');

# create a instance of request
$request = new CF_App_Controller_Request('/icafe/public/');
$request->setUrlParamsIndex(
	array(
		'module' 		=> 0,
		'controller' 	=> 1,
		'action'		=> 2
	)
);
CF_App::getInstance()->getControler()->setRequest($request);

$postRaw = (file_get_contents("php://input"));
if($postRaw){
	$postRaw = json_decode($postRaw, true);
	if($postRaw && $postRaw['action'] && $postRaw['method']){
		$extDirectResponse = new CF_App_Controller_Response_ExtDirect();
		CF_App::getInstance()->getControler()->setResponse($extDirectResponse);
		CF_App::getInstance()->getControler()->setController($postRaw['action'])
			->setAction($postRaw['method']);
			
		if($postRaw['data'] && is_array($postRaw['data'][0])){
			CF_App::getInstance()->getControler()
				->getRequest()->addParamSource($postRaw['data'][0]);
		}
	}
}

# because admin module used extjs lib, we do need render script, only render layout
CF_App::getInstance()->getView()->setRender(false);
CF_App::getInstance()->getView()->assign('ext_version', $configReader->ext_version);

# set application path
CF_App::getInstance()->setPath($appPath);

# authenticate
$auth = CF_Auth::getInstance();
if(!$auth->has() 
	&& 'language' != CF_App::getInstance()->getControler()->getController() 
	&& 'load' != CF_App::getInstance()->getControler()->getAction()
){
	CF_App::getInstance()->getControler()->setController('auth');
}

# run application
CF_App::getInstance()->run();
}catch(Exception $e){
	die(json_encode(array('error' => true, 'msg' => $e->getMessage())));
}